package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.util.Base64;
import com.fdb.basic.framework.core.util.DateUtility;
import com.fdb.basic.framework.core.util.FileUtil;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.efp.nls.service.facade.OverdueDownService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 
  * 百度文件下载 
  * @author ludayong
  * @since 2019年2月1日 下午6:36:14
  * @version 1.0
 */
@Service("overdueDownService")
public class OverdueDownServiceImpl implements OverdueDownService {
	
	private static Logger logger = LoggerFactory.getLogger(OverdueDownServiceImpl.class);
	
	private static String CTRL_SUFFIX = ".pdf";// 文件后缀

	@Override
	public Map downloadFile(String date,String locaFilePath,String ZXdownloadPath,String MYdownloadPath) throws Exception {
		int code = 0;
		String dateOf10 = DateUtility.format8To10(date);//yyyy-MM-dd
		String dateOf8 = DateUtility.format10To8(date);// 将10位日期转化成8位
		Map<String, String> resultMap = new HashMap<String, String>();
		Map<String, String> ZXResultMap = new HashMap<String, String>();
		Map<String, String> MYResultMap = new HashMap<String, String>();
		try {
			// 尊享贷
			String ZXdownload = ZXdownloadPath + dateOf8;
			logger.info("尊享贷下载文件路径：" + ZXdownload);
			File ZXfile = new File(ZXdownload);
			if (ZXfile.exists()) {
				logger.info("尊享贷下载文件开始！");
				ZXResultMap = down(dateOf10, ZXfile, ZXdownload, dateOf8,locaFilePath,ZXdownloadPath,"ZX");
			} else {
				logger.info("尊享贷当天文件夹不存在！");
				code = -2;
			}
			// 满意贷
			String MYdownload = MYdownloadPath + dateOf8;
			logger.info("满意贷下载文件路径：" + MYdownload);

			File MYfile = new File(MYdownload);
			if (MYfile.exists()) {
				logger.info("满意贷下载文件开始！");
				MYResultMap = down(dateOf10, MYfile, MYdownload, dateOf8,locaFilePath,MYdownloadPath,"MY");
			} else {
				logger.info("满意贷当天文件夹不存在！");
				code = -2;
			}
		} catch (FileNotFoundException e) {
			code = -1;
			logger.error("服务器下载文件出现异常：" + e.getMessage());
		} catch (IOException e) {
			code = -1;
			logger.error("服务器下载文件出现异常：" + e.getMessage());
		}
		if("0".equals(ZXResultMap.get("code"))) {
			ZXResultMap.remove("code");
			resultMap.putAll(ZXResultMap);
		}
		if("0".equals(MYResultMap.get("code"))) {
			MYResultMap.remove("code");
			resultMap.putAll(MYResultMap);
		}
		if(!resultMap.isEmpty()) {
			resultMap.put("code", "0");
		}else {
			resultMap.put("code", code+"");
		}
		return resultMap;
	}

	private Map<String, String> down(String createTime, File ZXfile, String zXdownload, String dateOf8,String locaFilePath,String path,String fileFlag)
			throws FileNotFoundException, IOException {
		int code = 0;
		int count = 0;
		File[] nfile = ZXfile.listFiles();
		Map<String, String> resultMap = new HashMap<String, String>();
		for (File f : nfile) { // 遍历文件夹内所有文件
			if (f.getName().endsWith(CTRL_SUFFIX)) { // 判断文件后缀是否为.pdf
				logger.info("文件下载地址为:" + f.getAbsolutePath());
				//把文件放入集合中返回
				String fileStrByte = fileToStrByte(f.getAbsolutePath());
				if(!StringUtil.isNullorBank(fileStrByte)) {
					resultMap.put(fileFlag+f.getName().replace(CTRL_SUFFIX, ""), fileStrByte);
					count++;
				}
				if (count > 0) {
					code = 0;
					logger.info("文件下载成功！");
				} else {
					code = -2;
					logger.info("当天无pdf文件！");
				}
			}
		}
		resultMap.put("code", code+"");
		return resultMap;
	}
	
	/**
	 * 
	 * 1、把文件转为二进制字符串<br>
	 * 
	 * @param filePath
	 * @param fileName
	 * @return
	 * @since 2019年2月1日 下午6:36:14
	 * @author:ludayong
	 */

	private String fileToStrByte(String filePath) {
		File file = new File(filePath);
		String fileStr = null;
		byte[] fileBytes = null;
		try {
			fileBytes = FileUtil.toByteArray(file);
		} catch (IOException e) {
			logger.error("文件转字节数组出现异常，异常信息" + e.getMessage());
		}
		fileStr = Base64.encodeBytes(fileBytes);
		return fileStr;
	}

	/**
	 * 
	 * 查询是否存在当天百度文件夹
	 * 
	 * @param date
	 * @param MYdownloadPath
	 * @param ZXdownloadPath
	 * @return
	 * @throws Exception
	 * @author hucy
	 * @since 2019年2月13日 下午3:40:58
	 * @version 1.0
	 */
	@Override
	public int QueryOverdueDown(String date, String MYdownloadPath, String ZXdownloadPath) throws Exception {
		int code;
		String dateOf8 = DateUtility.format10To8(date);// 将10位日期转化成8位
		try {
			// 尊享贷
			String ZXdownload = ZXdownloadPath + dateOf8;
			logger.info("尊享贷下载文件路径：" + ZXdownload);
			File ZXfile = new File(ZXdownload);
			if (ZXfile.exists()) {
				logger.info("尊享贷存在当天文件夹！");
				code = 0;
			} else {
				logger.info("尊享贷当天文件夹不存在！");
				code = -2;
			}
			// 满意贷
			String MYdownload = MYdownloadPath + dateOf8;
			logger.info("满意贷下载文件路径：" + MYdownload);
			File MYfile = new File(MYdownload);
			if (MYfile.exists()) {
				logger.info("满意贷存在当天文件夹！");
				code = 0;
			} else {
				logger.info("满意贷当天文件夹不存在！");
				code = code;
			}
		} catch (Exception e) {
			logger.error("查询文件发生异常!", e);
			code = -1;
		}
		return code;
	}
	
}
